#include"myname_pipe.hpp"
#define MAX_NUM 1024

//这个进程用于读取a和b发的数据
int main(){
    bool ret1 = creatfifo(PATH_NAME);//管道1
    bool ret2 = creatfifo(PATH_NAME2);//管道2
   //assert(ret1); assert(ret2);
   // (void)ret1; (void)ret2;

    int fp1 = open(PATH_NAME,O_RDONLY);
    if(fp1 < 0) exit(1);
    int fp2 = open(PATH_NAME2,O_RDONLY);
    if(fp2 < 0) exit(1);
    
    char buffer1[MAX_NUM];
    char buffer2[MAX_NUM];

    int real_read1 = read(fp1,buffer1,MAX_NUM);
    if(real_read1 > 0){
        printf("读取第一个选手的数据成功\n");
    }

    int real_read2 = read(fp1,buffer2,MAX_NUM);
    if(real_read2 > 0){
        printf("读取第二个选手的数据成功\n");
    }

    
    //开始判断结果
    int awin = 0;
    int bwin = 0;
    int n_win = 0;
    int res = 0;
    for(int i = 0;i < NUM_COMPARE;i++){
        printf("第%d回合：",i+1);
        res = judge(buffer1[i],buffer2[i]);
        if(res == '1'){
            printf("a获胜:%d\n",++awin);
        }else if(res == '2'){
            printf("b获胜:%d",++bwin);
        }else{
            printf("平局:%d\n",++n_win);
        }
    }

    printf("比赛结果公示:\n");
    printf("a选手赢了:%d次\n",awin);
    printf("b选手赢了:%d次\n",bwin);
    printf("平局:%d次\n",n_win);

     close(fp1);
     close(fp2);




    return 0;
}
